home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 November / EnigmA AMIGA RUN 02 (1995)(G.R. Edizioni)(IT)[!][issue 1995-11][Skylink CD].iso / earcd / unix / mp14tar.z / mp14tar / mpack / magic.c < prev    next >
C/C++ Source or Header  |  1994-06-01  |  3KB  |  77 lines

  1. /* (C) Copyright 1993 by John G. Myers
  2.  * All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software
  5.  * and its documentation for any purpose is hereby granted without
  6.  * fee, provided that the above copyright notice appear in all copies
  7.  * and that both that copyright notice and this permission notice
  8.  * appear in supporting documentation, and that the name of John G.
  9.  * Myers not be used in advertising or publicity pertaining to
  10.  * distribution of the software without specific, written prior
  11.  * permission.  John G. Myers makes no representations about the
  12.  * suitability of this software for any purpose.  It is provided "as
  13.  * is" without express or implied warranty.
  14.  *
  15.  * JOHN G. MYERS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
  16.  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17.  * FITNESS, IN NO EVENT SHALL JOHN G. MYERS BE LIABLE FOR ANY SPECIAL,
  18.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  19.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  20.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
  21.  * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  */
  23. #include <stdio.h>
  24.  
  25. /* Description of the various file formats and their magic numbers */
  26. struct magic {
  27.     char *name;            /* Name of the file format */
  28.     char *num;        /* The magic number */
  29.     int len;        /* Length of same (0 means strlen(magicnum)) */
  30. };
  31.  
  32. /* The magic numbers of the file formats we know about */
  33. static struct magic magic[] = {
  34.     { "image/gif", "GIF", 0 },
  35.     { "image/jpeg", "\377\330\377", 0 },
  36.     { "video/mpeg", "\0\0\001\263", 4 },
  37.     { "application/postscript", "%!", 0 },
  38. };
  39. static int num_magic = (sizeof(magic)/sizeof(magic[0]));
  40. static char *default_type = "application/octet-stream";
  41.  
  42. /* The longest magic number */
  43. static int max_magiclen = 0;
  44.  
  45. /*
  46.  * Determins the format of the file "inputf".  The name
  47.  * of the file format (or NULL on error) is returned.
  48.  */
  49. char *magic_look(infile)
  50. FILE *infile;
  51. {
  52.     int i, j;
  53.     char buf[80];
  54.     int numread = 0;
  55.  
  56.     if (max_magiclen == 0) {
  57.     for (i=0; i<num_magic; i++) {
  58.         if (magic[i].len == 0) magic[i].len = strlen(magic[i].num);
  59.         if (magic[i].len > max_magiclen) max_magiclen = magic[i].len;
  60.     }
  61.     }
  62.  
  63.     numread = fread(buf, 1, max_magiclen, infile);
  64.     rewind(infile);
  65.  
  66.     for (i=0; i<num_magic; i++) {
  67.     if (numread >= magic[i].len) {
  68.         for (j=0; j<magic[i].len; j++) {
  69.         if (buf[j] != magic[i].num[j]) break;
  70.         }
  71.         if (j == magic[i].len) return magic[i].name;
  72.     }
  73.     }
  74.  
  75.     return default_type;
  76. }
  77.